package com.antkit.mark.ak.wedigt;

import android.annotation.TargetApi;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.PaintFlagsDrawFilter;
import android.os.Build;
import android.os.Handler;
import android.os.Message;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import com.antkit.mark.ak.gd.XiaoBaiPlayer;

/**
 * @author Mark
 * @Date on 2019/1/24
 **/
public class InteractionCircleView extends View implements InteractionCircleInterface{

    private int progress;
    private int state;
    private int time;
    //up 的最大时间
    private int maxTimme =10;
    //到顶峰状态后需要坚持的时间
    private int holdTime = 3;
    //一组后需要放松的时间
    private int relaxTime = 3;

    public InteractionCircleView(Context context) {
        super(context);
    }

    public InteractionCircleView(Context context,  AttributeSet attrs) {
        super(context, attrs);
    }

    public InteractionCircleView(Context context,  AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    public InteractionCircleView(Context context,  AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
    }

    int width = 1000;
    int height = 1000;
    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
      /*  width = getWidth();
        height = getHeight();*/
    }

    @Override
    protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
        super.onLayout(changed, left, top, right, bottom);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        if (event.getAction() == MotionEvent.ACTION_DOWN){
            up();
        }
        return super.onTouchEvent(event);
    }

    Paint paint;
    @TargetApi(Build.VERSION_CODES.LOLLIPOP)
    @Override
    protected void onDraw(Canvas canvas) {
        PaintFlagsDrawFilter paintFlagsDrawFilter =
                new PaintFlagsDrawFilter(0, Paint.ANTI_ALIAS_FLAG);
        canvas.setDrawFilter(paintFlagsDrawFilter);

        super.onDraw(canvas);
        if (paint==null){
            paint = new Paint();
            paint.setColor(Color.WHITE);
            //设置抗锯齿
            paint.setAntiAlias(true);
        }

        paint.setStyle(Paint.Style.STROKE);
        paint.setStrokeWidth(5);
        canvas.drawCircle(width/2,width/2,width/4,paint);
        paint.setStyle(Paint.Style.FILL_AND_STROKE);
        paint.setColor(Color.RED);
        canvas.drawCircle(width/2,width/2,width/4-5,paint);
       // canvas.drawRoundRect(0, 0, 100, 100, 20, 20, paint);
        paint.setColor(Color.WHITE);
        paint.setStrokeWidth(2);
        paint.setTextSize(25);

        canvas.drawText(""+time,width/2,width/2,paint);
    }

    @Override
    public void showProgress(int progress) {

    }

    @Override
    public void showTime(int time) {

        invalidate();

    }

    @Override
    public void showState(int state) {

    }

    @Override
    public void up() {

        new XiaoBaiPlayer().playVoice(getContext(),1,1);
        state = 1;
        if (mHandler==null){
            mHandler = new Handler(){
                @Override
                public void handleMessage(Message msg) {
                    super.handleMessage(msg);
                    switch (msg.what){
                        case UP:
                            if (time<=maxTimme){

                                showTime(time);
                                time++;
                                mHandler.sendMessageDelayed(mHandler.obtainMessage(UP),1000);
                            }else{

                                mHandler.sendMessageDelayed(mHandler.obtainMessage(DOWN),holdTime*1000);

                            }

                            break;
                        case NORMAL:

                            break;
                        case DOWN:
                            if (time>0){

                                time--;
                                showTime(time);
                                mHandler.sendMessageDelayed(mHandler.obtainMessage(DOWN),1000);
                            }else{

                                mHandler.sendMessageDelayed(mHandler.obtainMessage(UP),relaxTime*1000);

                            }

                            break;
                    }
                }
            };
        }

        mHandler.sendEmptyMessage(UP);
    }
    Handler mHandler;
    final int UP = 1;
    final int DOWN = -1;

    final int NORMAL = 0;


    @Override
    public void down() {

        state = -1;

    }
}
